// eslint-disable-next-line import/named
import { MutationTree } from 'vuex'
import { WorkbenchStateInterface } from './state'

const mutation: MutationTree<WorkbenchStateInterface> = {
  pushEvent (state, event) {
    event = {
      ...event,
      id: state.events.length + 1
    }
    state.events = [
      event,
      ...state.events
    ]
  },
  initEvents (state, payload) {
    state.events = payload
  },
  setOptions (state, payload: {
    backgrounds: { id: string, url: string }[],
    backgroundId: string,
    images?: { id: string, url: string }[],
    backgroundImageId?: string,
    autoPlay: boolean
  }) {
    // 兼容旧版本 images
    if (!payload.backgrounds) {
      payload.backgrounds = payload.images || []
    }
    if (!payload.backgroundId) {
      payload.backgroundId = payload.backgroundImageId ||
        payload.backgrounds[0].id
    }
    delete payload.images
    delete payload.backgroundImageId
    state.options = payload
  }
}

export default mutation
